<?php
namespace app\tadmin\controller;
use WordAnalysis\Analysis;
use app\common\library\ContentApi;
/**
 * 工具控制器
 * Class Index
 * @package app\tadmin\controller
 */
class Util extends Controller {
    /**
     * 关键字
     */
    public function keywords(){
        $content = input('post.content','' , 'strip_tags');
        $data = ContentApi::keywords($content);
        $errormsg = '分析关键字成功';
        $tags = [];
        if(!$data){
            $errormsg = '分析关键字出错,错误码：'. ContentApi::$error;
        }else{
            $tags = array_slice($data , 0 , 4);
        }
        return $this->renderSuccess($errormsg , '' , ['tags'=>$tags]);
    }
    /**
     * 描述
     */
    public function desc(){
        $content = input('post.content','' , 'strip_tags');
        $errormsg = '分析描述成功';
        $desc = ContentApi::desc($content);
        if(!$desc){
            $errormsg .= '分析描述出错,错误码：'. ContentApi::$error;
        }
        return $this->renderSuccess($errormsg , '' , ['desc'=>$desc]);
    }
    /**
     * 标题
     */
    public function title(){
        $content = input('post.content','' , 'strip_tags');
        $errormsg = '分析标题成功';
        $title = ContentApi::title($content);
        if(!$title){
            $errormsg .= '分析标题出错,错误码：'. ContentApi::$error;
        }
        return $this->renderSuccess($errormsg , '' , ['title'=>$title]);
    }

    /**
     * 伪原创
     */
    public function rewrite(){
        $content = input('post.content','');
        $errormsg = '伪原创成功';
        $content = urlencode($content);
        $rewriteData = ContentApi::rewrite($content);
        if(!$rewriteData){
            $errormsg .= '分析描述出错,错误码：'. ContentApi::$error;
        }
        return $this->renderSuccess($errormsg , '' , ['redata'=>$rewriteData]);
    }
    /**
     * 下载图片
     */
    public function downimg(){
        $imgs = input('post.imgs');
        $weburl = config('myconfig.host_url');
        if($imgs != ''){
            $msg = [];
            $imgdata = [];
            $imgArr = explode(',' , $imgs);
            foreach($imgArr as $v){
                if($res = httpdown($v)){
                    //添加水印
                    $uploadFile = realpath(WEB_PATH) . '/' . $res;
                    $image = \think\Image::open($uploadFile);
                    $image->water(WEB_PATH.'assets/water.png',\think\Image::WATER_SOUTHEAST )->save($uploadFile);
                    $imgdata[] = ['src'=>$v , 'dest'=>$weburl . $res];
                }else{
                    $msg[] = '['.$v.']-下载失败';
                }
            }
            return $this->renderSuccess('下载图片成功' , '' , ['msg'=>implode(',' , $msg) , 'imgdata'=>$imgdata]);
        }
        return $this->renderError('没有图片下载');
    }
}